﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;

namespace Maxi.Models
{
    public static class Extensions
    {
        private static String KeyCrypto
        {
            get 
            {
                var bytes = Encoding.UTF8.GetBytes("ColegioMaxi");
                var base64 = Convert.ToBase64String(bytes);
                return base64; 
            }
        }
        public static string SuAluno
        {
            get
            {
                return "AL";
            }
        }

        public static string SuResponsavel
        {
            get
            {
                return "RP";
            }
        }

        public static string SuProfessor
        {
            get
            {
                return "PR";
            }
        }
        public static Int32 ToInt32(this Object obj)
        {
            if (obj == null || String.IsNullOrEmpty(obj.ToString()))
                return 0;
            try { return Convert.ToInt32(obj); }
            catch (Exception) { return 0; }
        }
        public static Int64 ToInt64(this Object obj)
        {
            if (obj == null || String.IsNullOrEmpty(obj.ToString()))
                return 0;
            try { return Convert.ToInt64(obj); }
            catch (Exception) { return 0; }
        }
        public static Decimal ToDecimal(this Object obj)
        {
            if (obj == null || String.IsNullOrEmpty(obj.ToString()))
                return 0;
            try { return Convert.ToDecimal(obj); }
            catch (Exception) { return 0; }
        }
        public static Double ToDouble(this Object obj)
        {
            if (obj == null || String.IsNullOrEmpty(obj.ToString()))
                return 0;
            try { return Convert.ToDouble(obj); }
            catch (Exception) { return 0; }
        }
        public static DateTime? ToDateTime(this string s)
        {
            DateTime dtr;
            var tryDtr = DateTime.TryParse(s, out dtr);
            return (tryDtr) ? dtr : new DateTime?();
        }
        public static string RemoverAcento(this string text)
        {
            var normalizedString = text.Normalize(NormalizationForm.FormD);
            var stringBuilder = new StringBuilder();

            foreach (var c in normalizedString)
            {
                var unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c);
                if (unicodeCategory != UnicodeCategory.NonSpacingMark)
                {
                    stringBuilder.Append(c);
                }
            }

            return stringBuilder.ToString().Normalize(NormalizationForm.FormC);
        }

        public static string Encrypt(this string input)
        {
            byte[] inputArray = UTF8Encoding.UTF8.GetBytes(input);
            TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
            tripleDES.Key = UTF8Encoding.UTF8.GetBytes(KeyCrypto);
            tripleDES.Mode = CipherMode.ECB;
            tripleDES.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = tripleDES.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length);
            tripleDES.Clear();
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

        public static string Decrypt(this string input)
        {
            byte[] inputArray = Convert.FromBase64String(input);
            TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
            tripleDES.Key = UTF8Encoding.UTF8.GetBytes(KeyCrypto);
            tripleDES.Mode = CipherMode.ECB;
            tripleDES.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = tripleDES.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length);
            tripleDES.Clear();
            return UTF8Encoding.UTF8.GetString(resultArray);
        }

        public static string GerarSenha()
        {
            Random rnd = new Random();
            //string senha = rnd.Next(10000, 99999).ToString();
            string senha = "123";
            return senha.Encrypt();
        }
    }
}